package org.example.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {

    private static final String SING = "!RANSHIYAN";

    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 7);

        JWTCreator.Builder builder = JWT.create();
        //payload
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });

        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SING));
        return  token;
    }

    //验证token 合法性
    public static void verify(String token){
        DecodedJWT build = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

    public static DecodedJWT tokeInfo(String token){
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

    /*//token获取信息
    public static String Deco(DecodedJWT decodedJWT){
        Map<String, Claim> claims = decodedJWT.getClaims();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.forEach((k,v)->{
            System.out.println(k+","+v);
        });
    }*/
}
